package com.jchz.net.util;

import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;


public class CountDownUtils {
    private long day = 0;
    private long hour = 0;
    private long minute = 0;
    private long second = 0;

    private boolean dayNotAlready = false;
    private boolean hourNotAlready = false;
    private boolean minuteNotAlready = false;
    private boolean secondNotAlready = false;
    private TextView textView;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (textView != null) {
                if (lessThanOneHour(getCurDuration())) {
                    textView.setText(Html.fromHtml("<font color='#ed1c24'>" + getCurDuration() + "</font>"));
                } else {
                    textView.setText(getCurDuration());
                }

            }
        }
    };

    public CountDownUtils(long totalSeconds, TextView textView) {
        this.textView = textView;
        initData(totalSeconds);
    }

    public void startCount() {
        new Timer().schedule(new TimerTask() {
            public void run() {
                if (secondNotAlready) {
                    handler.sendEmptyMessage(0);
                } else {
                    cancel();
                }

            }
        }, 0, 1000);
    }

    /**
     * 初始化赋值
     *
     * @param totalSeconds
     */
    private void initData(long totalSeconds) {
        resetData();

        if (totalSeconds > 0) {
            secondNotAlready = true;
            second = totalSeconds;
            if (second >= 60) {
                minuteNotAlready = true;
                minute = second / 60;
                second = second % 60;
                if (minute >= 60) {
                    hourNotAlready = true;
                    hour = minute / 60;
                    minute = minute % 60;
                    if (hour > 24) {
                        dayNotAlready = true;
                        day = hour / 24;
                        hour = hour % 24;
                    }
                }
            }
        }
    }

    public void resetData() {
        day = 0;
        hour = 0;
        minute = 0;
        second = 0;
        dayNotAlready = false;
        hourNotAlready = false;
        minuteNotAlready = false;
        secondNotAlready = false;
    }


    public void releaseAll() {
        day = 0;
        hour = 0;
        minute = 0;
        second = 0;
        dayNotAlready = false;
        hourNotAlready = false;
        minuteNotAlready = false;
        secondNotAlready = false;
        textView = null;
    }


    private String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }


    /**
     * 计算各个值的变动
     */
    public String getCurDuration() {
        if (secondNotAlready) {
            if (second > 0) {
                second--;
                if (second == 0 && !minuteNotAlready) {
                    secondNotAlready = false;
                }
            } else {
                if (minuteNotAlready) {
                    if (minute > 0) {
                        minute--;
                        second = 59;
                        if (minute == 0 && !hourNotAlready) {
                            minuteNotAlready = false;
                        }

                    } else {
                        if (hourNotAlready) {
                            if (hour > 0) {
                                hour--;
                                minute = 59;
                                second = 59;
                                if (hour == 0 && !dayNotAlready) {
                                    hourNotAlready = false;
                                }

                            } else {
                                if (dayNotAlready) {
                                    day--;
                                    hour = 23;
                                    minute = 59;
                                    second = 59;
                                    if (day == 0) {
                                        dayNotAlready = false;
                                    }

                                }
                            }
                        }
                    }

                }
            }

        }
        return "剩" + (TypeCastHelper.toInt(hour) * 60 + TypeCastHelper.toInt(minute)) + "分";
        // return unitFormat(TypeCastHelper.toInt(hour)) + ":" + unitFormat(TypeCastHelper.toInt(minute)) + ":" + unitFormat(TypeCastHelper.toInt(second));
    }


    public void showTime(String curDuration) {
        String[] split = curDuration.split(":");
        StringBuffer setText = new StringBuffer();
        for (int i = 0; i < split.length; i++) {
            if (i == 0) {
                setText.append(split[0] + "小时");
            }
            if (i == 1) {
                setText.append(split[1] + "分钟");
            }
            if (i == 2) {
                setText.append(split[2] + "秒");
            }
        }
    }


    /**
     * 是否小于1小时
     *
     * @param curDuration
     * @return
     */
    public boolean lessThanOneHour(String curDuration) {
        String[] split = curDuration.split(":");
        for (int i = 0; i < split.length; i++) {
            if (i == 0 && TypeCastHelper.toInt(split[0]) < 1) {
                return true;
            }
        }
        return false;
    }
}